 
 
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>代码高亮文档 - Layui</title>
<meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="apple-mobile-web-app-status-bar-style" content="black"> 
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="format-detection" content="telephone=no">
  <script>
  ;!function(){self!==parent&&(location.href="//www.baidu.com/")}();
  </script>
  <link rel="stylesheet" href="../../layui/dist/css/layui.css" media="all">
  <link rel="stylesheet" href="../../static/css/global.css" media="all">
</head>
<body><div class="layui-header header header-doc" autumn="">
  <div class="layui-container">
    <a class="logo" href="../../index.html">
      <img src="../../static/images/layui/logo.png" alt="layui">
    </a>
    <div class="layui-form layui-hide-xs component" lay-filter="LAY-site-header-component"></div>
    <div class="layui-hide-xs site-notice"></div>
    
    <ul class="layui-nav" id="LAY_NAV_TOP">
      <li class="layui-nav-item layui-this">
        <a href="../index.html">文档</a> 
      </li>
      <li class="layui-nav-item ">
        <a href="../../demo/index.html">示例</a>
      </li>
      
      <li class="layui-nav-item">
        <a href="javascript:;">
          <!--<span class="layui-badge-dot" style="left:0; right: auto; margin: -4px 0 0 5px;"></span>--> 
          周边
        </a>
        <dl class="layui-nav-child layui-nav-child-c">  
          <dd class="layui-hide-sm layui-show-xs" lay-unselect="">
            <a href="javascript:window.open('https://gitee.com/sentsin/layui/issues');" target="_blank" rel="nofollow">问题反馈</a>
            <hr>
          </dd>
          
          <dd lay-unselect=""><a href="../../alone.html" target="_blank" lay-unselect="">独立组件</a></dd>
          <dd lay-unselect=""><a href="../../extend/index.html" target="_blank">扩展组件</a></dd>
        </dl>
      </li>
      
      
    </ul>
  </div>
</div>
 
<!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
<!--[if lt IE 9]>
  <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
  <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
 
<ul class="site-dir">
  <li><a href="#use"><cite>使用</cite></a></li>
  <li><a href="#params"><cite>基础参数</cite></a></li>
  <li><a href="#elem"><cite>指定元素</cite></a></li>
  <li><a href="#title"><cite>设置标题</cite></a></li>
  <li><a href="#height"><cite>设置最大高度</cite></a></li>
  <li><a href="#encode"><cite>转义html标签</cite></a></li>
  <li><a href="#skin"><cite>风格选择</cite></a></li>
</ul>
<div class="layui-container layui-row">
  
<div class="layui-col-md3">
  <div class="layui-panel site-menu">
    <ul class="layui-menu layui-menu-lg">
      <li class="layui-menu-item-group" lay-options="{type: 'group', isAllowSpread: true}">
        <div class="layui-menu-body-title">
          基础说明
        </div>
        <hr>
        <ul>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../index.html">
                <span>开始使用 </span> 
                <span class="layui-font-12 layui-font-gray">Getting Started</span>
              </a>
              
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../base/infrastructure.html">
                <span>底层方法 </span>
                <span class="layui-font-12 layui-font-gray">基础支撑</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../base/element.html">
                <span>页面元素 </span>
                <span class="layui-font-12 layui-font-gray">规范 公共类 属性</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../base/modules.html">
                <span>模块规范 </span>
                <span class="layui-font-12 layui-font-gray">使用 扩展</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../base/faq.html">
                <span>常见问题 </span>
                <span class="layui-font-12 layui-font-gray">FAQ</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../base/changelog.html">
                <span>更新日志 </span>
                <span class="layui-font-12 layui-font-gray">changelog</span>
                
                  <span class="layui-badge-dot"></span>
                
              </a>
            </div>
          </li>
        </ul>
      </li>
      
      <li class="layui-menu-item-group" lay-options="{type: 'group', isAllowSpread: true}">
        <div class="layui-menu-body-title">
          页面元素
        </div>
        <hr>
        <ul>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/layout.html">
                <span>布局 </span>
                <span class="layui-font-12 layui-font-gray">栅格 后台管理布局</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/color.html">
                <span>颜色 </span>
                <span class="layui-font-12 layui-font-gray">主色调 颜色搭配</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/icon.html">
                <span>图标 </span>
                <span class="layui-font-12 layui-font-gray">iconfont 字体图标</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/anim.html">
                <span>动画 </span>
                <span class="layui-font-12 layui-font-gray">内置 CSS3 动画</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/button.html">
                <span>按钮 </span>
                <span class="layui-font-12 layui-font-gray">button</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/form.html">
                <span>表单 </span>
                <span class="layui-font-12 layui-font-gray">form 元素集合</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/nav.html">
                <span>导航 </span>
                <span class="layui-font-12 layui-font-gray">nav 面包屑</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/menu.html">
                <span>菜单 </span>
                <span class="layui-font-12 layui-font-gray">menu 基础菜单</span>
                
                  <span class="layui-badge-dot"></span>
                
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/tab.html">
                <span>选项卡 </span>
                <span class="layui-font-12 layui-font-gray">Tabs 切换</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/progress.html">
                <span>进度条 </span>
                <span class="layui-font-12 layui-font-gray">progress</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/panel.html">
                <span>面板 </span>
                <span class="layui-font-12 layui-font-gray">panel 卡片 折叠</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/table.html">
                <span>表格 </span>
                <span class="layui-font-12 layui-font-gray">静态 table</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/badge.html">
                <span>徽章 </span>
                <span class="layui-font-12 layui-font-gray">小圆点  小边框</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/timeline.html">
                <span>时间线 </span>
                <span class="layui-font-12 layui-font-gray">timeline</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/auxiliar.html">
                <span>辅助 </span>
                <span class="layui-font-12 layui-font-gray">引用 字段集 横线等</span>
              </a>
            </div>
          </li>
        </ul>
      </li>
      
      <li class="layui-menu-item-group" lay-options="{type: 'group', isAllowSpread: true}">
        <div class="layui-menu-body-title">
          内置模块
        </div>
        <hr>
        <ul>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="layer.html">
                <span>弹出层 </span>
                <span class="layui-font-12 layui-font-gray">layer</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="laydate.html">
                <span>日期与时间选择 </span>
                <span class="layui-font-12 layui-font-gray">laydate</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="laypage.html">
                <span>分页 </span>
                <span class="layui-font-12 layui-font-gray">laypage</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="laytpl.html">
                <span>模板引擎 </span>
                <span class="layui-font-12 layui-font-gray">laytpl</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="table.html">
                <span>数据表格 </span>
                <span class="layui-font-12 layui-font-gray">table</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="form.html">
                <span>表单 </span>
                <span class="layui-font-12 layui-font-gray">form</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="upload.html">
                <span>文件上传 </span>
                <span class="layui-font-12 layui-font-gray">upload</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="dropdown.html">
                <span>下拉菜单 </span>
                <span class="layui-font-12 layui-font-gray">dropdown</span>
                
                  <span class="layui-badge-dot"></span>
                
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="transfer.html">
                <span>穿梭框 </span>
                <span class="layui-font-12 layui-font-gray">transfer</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="tree.html">
                <span>树形组件 </span>
                <span class="layui-font-12 layui-font-gray">tree</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="colorpicker.html">
                <span>颜色选择器 </span>
                <span class="layui-font-12 layui-font-gray">colorpicker</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="element.html">
                <span>常用元素操作 </span>
                <span class="layui-font-12 layui-font-gray">element</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="slider.html">
                <span>滑块 </span>
                <span class="layui-font-12 layui-font-gray">slider</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="rate.html">
                <span>评分 </span>
                <span class="layui-font-12 layui-font-gray">rate</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="carousel.html">
                <span>轮播 </span>
                <span class="layui-font-12 layui-font-gray">carousel</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="flow.html">
                <span>流加载 </span>
                <span class="layui-font-12 layui-font-gray">flow</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="util.html">
                <span>工具组件 </span>
                <span class="layui-font-12 layui-font-gray">util</span>
              </a>
            </div>
          </li>
          <li class="layui-menu-item-checked2">
            <div class="layui-menu-body-title">
              <a href="">
                <span>代码高亮显示 </span>
                <span class="layui-font-12 layui-font-gray">code</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="layedit.html">
                <span>富文本编辑器 </span>
                <span class="layui-font-12 layui-font-gray">layedit</span>
              </a>
            </div>
          </li>
        </ul>
      </li>
    </ul>
  </div>
  <div class="layui-hide-v"> - </div>
</div>

  
  <div class="layui-col-md9 site-content">
    <h1 class="site-h1"><i class="layui-icon">&#xe635;</i> 代码高亮文档 - layui.code</h1>
    <blockquote class="site-text layui-elem-quote">
      该组件暂时只对你的 <em>pre</em> 元素进行一个修饰，从而保证你展现的代码更具可读性。目前它没有对不同的语言进行颜色高亮（后续逐步强化），但这丝毫不会影响它对你带来的便捷。
    </blockquote>
    <blockquote class="site-text layui-elem-quote">
      <p>模块加载名称：<em>code</em></p>
    </blockquote>
    
    
    <div class="site-title">
      <a name="use"></a>
      <fieldset><legend>使用</legend></fieldset>
    </div>
    <div class="site-text">
      <p>code模块的使用非常简单，请直接看代码，假设你在页面有这样一段pre标签：</p>
      <pre class="layui-code" lay-title="html">
&lt;pre class="layui-code">
//代码区域
var a = 'hello layui';
&lt;/pre>      
      </pre>
      <p>那么你只需要经过下面的方式：</p>
      <pre class="layui-code" lay-title="JavaScript">
layui.use('code', function(){ //加载code模块
  layui.code(); //引用code方法
});
      </pre>
      <p>就可以将那段pre区块显示成你现在看到的这个样子：</p>
      <pre class="layui-code">
//代码区域
var a = 'hello layui';
      </pre>
    </div>
    
    <div class="site-title">
      <a name="params"></a>
      <fieldset><legend>基础参数</legend></fieldset>
    </div>
    <div class="site-text">
      <p>方法：<a>layui.code(options)</a><br>它接受一个对象参数options，支持以下key的设定</p>
      <table class="site-table">
        <thead>
          <tr>
            <th>参数</th>
            <th>类型</th>
            <th>描述</th>
          </tr> 
        </thead>
        <tbody>
          <tr>
            <td>elem</td>
            <td>string</td>
            <td>指定元素的选择器</td>
          </tr>
          <tr>
            <td>title</td>
            <td>string</td>
            <td>设定标题</td>
          </tr>
          <tr>
            <td>height</td>
            <td>string</td>
            <td>设置最大高度</td>
          </tr>
          <tr>
            <td>encode</td>
            <td>boolean</td>
            <td>是否转义html标签，默认false</td>
          </tr>
          <tr>
            <td>skin</td>
            <td>string</td>
            <td>风格选择（值见下文）</td>
          </tr>
          <tr>
            <td>about</td>
            <td>boolean</td>
            <td>是否剔除右上关于</td>
          </tr>
        </tbody>
      </table>
      <p><a>特别提示：</a><em>除了上述方式的设置，我们还允许你直接在pre标签上设置属性来替代，如：</em></p>
      <pre class="layui-code">
&lt;pre class="layui-code" lay-title="" lay-height="" lay-skin="" lay-encode="">
这样有木有觉得更方便些
&lt;/pre>     
      </pre>
      <p>下面将针对每一个参数做进一步讲解。</p>
    </div>
    
    <div class="site-title">
      <a name="elem"></a>
      <fieldset><legend>指定元素</legend></fieldset>
    </div>
    <div class="site-text">
      <p>code模块会去自动查找class为<a>layui-code</a>的类，如果你初始给的不是该类，仅仅只是一个pre标签，那么需要通过elem设置选择器来指向元素：</p>
      <pre class="layui-code" lay-title="JavaScript">
layui.code({
  elem: 'pre' //默认值为<a>.layui-code</a>
});
      </pre>
    </div>
    
    <div class="site-title">
      <a name="title"></a>
      <fieldset><legend>设置标题</legend></fieldset>
    </div>
    <div class="site-text">
      <p>即左上角显示的文本，默认值为code</p>
      <pre class="layui-code" lay-title="JavaScript">
layui.code({
  title: 'JavaScript'
});
      </pre>
      <p>或者直接在pre标签上设置属性<a>&lt;pre lay-title="JavaScript">&lt;/pre></a></p>
    </div>
    
    <div class="site-title">
      <a name="height"></a>
      <fieldset><legend>设置最大高度</legend></fieldset>
    </div>
    <div class="site-text">
      <p>你可以设置以下key来控制修饰器的最大高度。如果内容低于该高度，则会自适应内容高度；如果内容超过了该高度，则会自动出现滚动条。</p>
      <pre class="layui-code" lay-title="JavaScript" lay-height="100px">
layui.code({
  height: '100px' //请注意必须加px。如果该key不设定，则会自适应高度，且不会出现滚动条。
});
 
 
 
 
 Hi，我是充数的 ^_^
 
      </pre>
      <p>或者直接在pre标签上设置属性<a>&lt;pre lay-height="100px">&lt;/pre></a></p>
    </div>
    <div class="site-title">
      <a name="encode"></a>
      <fieldset><legend>转义html标签</legend></fieldset>
    </div>
    <div class="site-text">
      <p>事实上很多时候你都需要在pre标签中展现html标签，而不希望它被浏览器解析。那么code模块允许你这么做，只需要开启encode即可，如：</p>
      <pre class="layui-code" lay-title="JavaScript">
layui.code({
  encode: true //是否转义html标签。默认不开启
});
      </pre>
      <p>开启了encode后的效果如下：</p>
      <pre class="layui-code" lay-title="显示html" lay-encode="true">
<ul>
  <li>HTML将不会被解析</li>
  <li>有木有感觉非常方便</li>
</ul>
<script>
!function(){
  var a = 123;
}();
</script>
      </pre>
      <p>或者直接在pre标签上设置属性<a>&lt;pre lay-encode="true">&lt;/pre></a></p>
    </div>
    
    <div class="site-title">
      <a name="skin"></a>
      <fieldset><legend>风格选择</legend></fieldset>
    </div>
    <div class="site-text">
      <p>你肯定不会满足于code的某一种显示风格，而skin参数则允许你设定许多种显示风格，我们目前内置了两种，分别为默认和notepad</p>
      <pre class="layui-code" lay-title="JavaScript">
layui.code({
  title: 'NotePad++的风格'
  ,skin: 'notepad' //如果要默认风格，不用设定该key。
});
      </pre>
      <p>上述的设定后，你会看到下面的样子</p>
      <pre class="layui-code" lay-title="NotePad++的风格" lay-skin="notepad">
i'm code.
i'm code too.     
      </pre>
      <p>或者直接在pre标签上设置属性<a>&lt;pre lay-skin="notepad">&lt;/pre></a></p>
    </div>
    
    <div class="layui-elem-quote">
  <p>layui - 在每一个细节中，用心与你沟通</p>
</div>
    
  </div>
</div>
  
<script>
window.global = {
  pageType: 'doc'
  ,preview: function(){
    var preview = document.getElementById('LAY_preview');
    return preview ? preview.innerHTML : '';
  }()
};
</script>
<script async="" src="../../pagead/js/adsbygoogle.js"></script>
<div class="site-tree-mobile layui-hide">
  <i class="layui-icon layui-icon-spread-left"></i>
</div>
<div class="site-mobile-shade"></div>
 
<script src="../../layui/dist/layui.js" ></script>
<script>
layui.config({
  base: '../../static/lay/modules/layui/'
  ,version: '1632428048260'
}).use('global');
</script>
 </body>
</html>